package 算法.NiuKe.TB;

import 算法.NiuKe.TreeNode;

import java.util.ArrayDeque;
import java.util.Queue;

/**
 * @author:谢君臣
 * @Date:2021/5/38:55
 * @version:1.0
 * @function:
 */
public class NC4 {
    public int sumNumbers(TreeNode root) {
        // write code here
        if (root==null)
            return 0;
        int sum = 0;
        Queue<TreeNode> queue = new ArrayDeque<>();
        queue.add(root);
        while (!queue.isEmpty()){
            TreeNode poll = queue.poll();
            if (poll.left== poll.right){
                sum += poll.val;
            }
            else {
                if (poll.left!=null) {
                    poll.left.val = poll.val*10 + poll.left.val;
                    queue.add(poll.left);
                }
                if (poll.right!=null) {
                    poll.right.val = poll.val*10 + poll.right.val;
                    queue.add(poll.right);
                }
            }
        }
        return sum;
    }
}
